application/config/routes.php
$route['api/v1/url/(:any)']['GET'] = 'api/v1/url/get/$1';
application/controllers/api/v1/url.php
public function get($url_shorten) {
$database = $this->load->database('default',true); //設定database連線
$query = $database //檢查紀錄是否存在
->from('urls')
->where('url_shorten',$url_shorten)
->get();
if (!$query -> num_rows() > 0) { //不大於零: 不存在
http_response_code(404); //回應HTTP 404 Not Found
print_r(
json_encode(
array(
'status' => 'error',
'code' => 404,
'message' => 'not-found'
)
)
);
die();
} else {
echo json_encode(
array(
'status' => 'success',
'code' => 200,
'data' => $query->result_array()
)
);
}
}